package com.aceleeyy.apb.generator;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class GenUtils {

    public static void createService(String className, String packageName) {
        String folder = System.getProperty("user.dir") + "/apb-bizcommon/src/main/java/" + packageName + "/service/impl/";
        File file = new File(folder);
        if (!file.exists()) {
            file.mkdirs();
        }
        String fileName = folder + className + "Service.java";

        try {
            File newdao = new File(fileName);
            FileWriter fw = new FileWriter(newdao);
            fw.write("package\t" + packageName.replace("/", ".") + ".service.impl;\r\n\n");
            fw.write("import org.springframework.stereotype.Service;\n\n");
            fw.write("import " + packageName.replace("/", ".") + ".base.BaseService;\n\n");
            fw.write("import " + packageName.replace("/", ".") + ".model." + className + ";\n");
            fw.write("import " + packageName.replace("/", ".") + ".mapper." + className + "Mapper;\n");
            fw.write("import " + packageName.replace("/", ".") + ".service.I" + className + "Service;\n\n");
            fw.write("@Service\n");
            fw.write("public class " + className + "Service extends BaseService<" + className + "Mapper, " + className + "> implements I" + className + "Service {\n\n");
            fw.write("}\n");
            fw.flush();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void createIService(String className, String packageName) {
        String folder = System.getProperty("user.dir") + "/apb-bizcommon/src/main/java/" + packageName + "/service/";
        File file = new File(folder);
        if (!file.exists()) {
            file.mkdirs();
        }

        String fileName = folder + "I" + className + "Service.java";

        try {
            File newdao = new File(fileName);
            FileWriter fw = new FileWriter(newdao);
            fw.write("package\t" + packageName.replace("/", ".") + ".service;\r\n\n");
            fw.write("import\t" + packageName.replace("/", ".") + ".base.IBaseService;\n");
            fw.write("import\t" + packageName.replace("/", ".") + ".model." + className + ";\n\n");
            fw.write("public interface I" + className + "Service extends IBaseService<" + className + "> {\n");
            fw.write("}\n");
            fw.flush();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void createMapper(String className, String packageName) {
        String folder = System.getProperty("user.dir") + "/apb-bizcommon/src/main/java/" + packageName + "/mapper/";
        File file = new File(folder);
        if (!file.exists()) {
            file.mkdirs();
        }

        String fileName = folder + className + "Mapper.java";

        try {
            File newdao = new File(fileName);
            FileWriter fw = new FileWriter(newdao);
            fw.write("package\t" + packageName.replace("/", ".") + ".mapper;\r\n");
            fw.write("import com.baomidou.mybatisplus.core.mapper.BaseMapper;\n\n");
            fw.write("import\t" + packageName.replace("/", ".") + ".model." + className + ";\n\n");
            fw.write("public interface " + className + "Mapper extends BaseMapper<" + className + "> {\n");
            fw.write("}\n");
            fw.flush();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
